package com.didibigdata.Repositories;

import com.didibigdata.entities.WorkTime;
import com.didibigdata.entities.Work;
import com.didibigdata.utils.JDBCTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lkabb on 2020/6/7
 */
public class WorkTimeRepository {

    /**
     * 实现读取数据操作，返回结果列表
     * @return List<Work>
     */
    public List<Work> getWorkList() {
        //日志
        Logger logger = LoggerFactory.getLogger(WorkTimeRepository.class);
        logger.info("add WorkTimeRepository logger successful!");

        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resOfWorkTime = null;
        List<WorkTime> workTimeList = new ArrayList<WorkTime>();
        List<Work> workList = new ArrayList<Work>();
        try {
            connection = JDBCTools.getConnection();
            String sqlWorkTime = "select day,count,worktime from t_didi2016_dayworktime";
            preparedStatement = connection.prepareStatement(sqlWorkTime);
            logger.info("connect to JDBC and executeQuery [{}]", sqlWorkTime);
            resOfWorkTime = preparedStatement.executeQuery();

            //初始化订单时长范围数组
            int[][] countOfSection = new int[3][11];

            //计算不同时期的订单数量
            int acount1 = 0;
            int acount2 = 0;
            int acount3 = 0;

            WorkTime workTime = null;
            while (resOfWorkTime.next()) {
                workTime = new WorkTime();
                //生成订单时长List
                workTime.setDay(resOfWorkTime.getString(1));
                workTime.setCount(Integer.parseInt(resOfWorkTime.getString(2)));
                workTime.setWorktime(resOfWorkTime.getString(3));
                workTimeList.add(workTime);

                //原始日期
                int day1 = Integer.parseInt(workTime.getDay());
                //定义几号
                int day2;
                if (day1 < 20161100) {
                    //十月的日期
                    day2 = day1 - 20161000;
                } else {
                    //十一月的日期
                    day2 = day1 - 20161100 + 31;
                }
                //定义星期几
                int day3 = day2 % 7;

                //不同时段，不同工作时长的订单数量
                if((day2 > 7) && ((day3==3) || (day3==4) || (day3==5) || (day3==6) || (day3==0))) {  //工作日
                    acount1++;
                    switch (workTime.getWorktime()) {
                        case "0to20":
                            countOfSection[0][0] += workTime.getCount();
                            break;
                        case "20to40":
                            countOfSection[0][1] += workTime.getCount();
                            break;
                        case "40to60":
                            countOfSection[0][2] += workTime.getCount();
                            break;
                        case "60to80":
                            countOfSection[0][3] += workTime.getCount();
                            break;
                        case "80to100":
                            countOfSection[0][4] += workTime.getCount();
                            break;
                        case "100to120":
                            countOfSection[0][5] += workTime.getCount();
                            break;
                        case "120to140":
                            countOfSection[0][6] += workTime.getCount();
                            break;
                        case "140to160":
                            countOfSection[0][7] += workTime.getCount();
                            break;
                        case "160to180":
                            countOfSection[0][8] += workTime.getCount();
                            break;
                        case "180to200":
                            countOfSection[0][9] += workTime.getCount();
                            break;
                        case "200to":
                            countOfSection[0][10] += workTime.getCount();
                            break;
                    }
                }
                else if((day2 > 7) && ((day3==1) || (day3==2))) { //周末
                    acount2++;
                    switch (workTime.getWorktime()) {
                        case "0to20":
                            countOfSection[1][0] += workTime.getCount();
                            break;
                        case "20to40":
                            countOfSection[1][1] += workTime.getCount();
                            break;
                        case "40to60":
                            countOfSection[1][2] += workTime.getCount();
                            break;
                        case "60to80":
                            countOfSection[1][3] += workTime.getCount();
                            break;
                        case "80to100":
                            countOfSection[1][4] += workTime.getCount();
                            break;
                        case "100to120":
                            countOfSection[1][5] += workTime.getCount();
                            break;
                        case "120to140":
                            countOfSection[1][6] += workTime.getCount();
                            break;
                        case "140to160":
                            countOfSection[1][7] += workTime.getCount();
                            break;
                        case "160to180":
                            countOfSection[1][8] += workTime.getCount();
                            break;
                        case "180to200":
                            countOfSection[1][9] += workTime.getCount();
                            break;
                        case "200to":
                            countOfSection[1][10] += workTime.getCount();
                            break;
                    }
                }
                else if (day2 >= 1 && day2 <= 7){ //假期
                    acount3++;
                    switch (workTime.getWorktime()) {
                        case "0to20":
                            countOfSection[2][0] += workTime.getCount();
                            break;
                        case "20to40":
                            countOfSection[2][1] += workTime.getCount();
                            break;
                        case "40to60":
                            countOfSection[2][2] += workTime.getCount();
                            break;
                        case "60to80":
                            countOfSection[2][3] += workTime.getCount();
                            break;
                        case "80to100":
                            countOfSection[2][4] += workTime.getCount();
                            break;
                        case "100to120":
                            countOfSection[2][5] += workTime.getCount();
                            break;
                        case "120to140":
                            countOfSection[2][6] += workTime.getCount();
                            break;
                        case "140to160":
                            countOfSection[2][7] += workTime.getCount();
                            break;
                        case "160to180":
                            countOfSection[2][8] += workTime.getCount();
                            break;
                        case "180to200":
                            countOfSection[2][9] += workTime.getCount();
                            break;
                        case "200to":
                            countOfSection[2][10] += workTime.getCount();
                            break;
                    }
                }
            }
            logger.info("count of workday [{}]", acount1);
            logger.info("count of weekend [{}]", acount2);
            logger.info("count of holiday [{}]", acount3);

            //计算平均值
            for (int i = 0; i < 11; i++) {
                countOfSection[0][i] /= acount1;
            }
            for (int i = 0; i < 11; i++) {
                countOfSection[1][i] /= acount2;
            }
            for (int i = 0; i < 11; i++) {
                countOfSection[2][i] /= acount3;
            }

            //生成work列表
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 11; j++) {
                    Work work = new Work();
                    work.setTypeCount(countOfSection[i][j]);
                    switch (j + 1) {
                        case 1:
                            work.setType("0-20分钟");
                            break;
                        case 2:
                            work.setType("20-40分钟");
                            break;
                        case 3:
                            work.setType("40-60分钟");
                            break;
                        case 4:
                            work.setType("60-80分钟");
                            break;
                        case 5:
                            work.setType("80-100分钟");
                            break;
                        case 6:
                            work.setType("100-120分钟");
                            break;
                        case 7:
                            work.setType("120-140分钟");
                            break;
                        case 8:
                            work.setType("140-160分钟");
                            break;
                        case 9:
                            work.setType("160-180分钟");
                            break;
                        case 10:
                            work.setType("180-200分钟");
                            break;
                        case 11:
                            work.setType("200分钟以上");
                            break;
                    }
                    workList.add(work);
                }
            }
            logger.info("set work list successful!");
        } catch (SQLException e) {
            logger.error("WorkTimeRepository SQLException: [{}]", e);
            e.printStackTrace();
        } finally {
            JDBCTools.releaseConnection(connection, preparedStatement, resOfWorkTime);
        }

        return workList;
    }

}
